<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
            list-style: none;
        }

        .fdj {
            width: 1200px;
            display: flex;
            margin: auto;
        }

        .left {
            width: 300px;
            height: 300px;
            position: relative;
        }

        .left img {
            width: 300px;
            height: 300px;
        }

        .cover {
            width: 100px;
            height: 100px;
            background-color: rgba(0, 0, 0, .5);
            position: absolute;
            top: 0;
            left: 0;
            opacity: 0;

            /* 不然成为目标源 */
            pointer-events: none;
        }

        .right {
            width: 400px;
            height: 400px;
            overflow: hidden;
            position: relative;
            margin-left: 150px;
            opacity: 0;
        }

        .right img {
            width: 1200px;
            height: 1200px;
            position: absolute;
        }
    </style>
</head>

<body>
    <div class="fdj">
        <div class="left">
            <img src="../images/girlsmall1.jpg" alt="">
            <div class="cover"></div>
        </div>
        <div class="right">
            <img src="../images/girlsmall1.jpg" alt="">
        </div>
    </div>


    <script>

        // 放大镜 
        //    css部分
        //       左边图片的大小 / 遮罩层的大小的比例  =  右边大图的大小 / 盒子的大小
        //       隐藏元素需要用  visibility:hidden / opacity:0    不能使用display:none

        //    js部分
        //      遮罩层和大图的移动方向是相反的，且成比例
        //         大图的移动范围 / 遮罩层的移动范围


        var oFdj = document.querySelector('.fdj');

        var oLeft = oFdj.querySelector('.left');
        var oCover = oFdj.querySelector('.cover');
        var oRight = oFdj.querySelector('.right');
        var oRightImg = oFdj.querySelector('.right img');



        var r = oCover.offsetWidth / 2;
        var maxX = oLeft.clientWidth - oCover.offsetWidth;
        var maxY = oLeft.clientHeight - oCover.offsetHeight;


        var rightMaxX = oRightImg.clientWidth - oRight.offsetWidth;
        var rightMaxY = oRightImg.clientHeight - oRight.offsetHeight;


        var scale = rightMaxX / maxX;
        console.log(scale);


        // 鼠标移入移出
        oLeft.onmouseover = function () {
            oCover.style.opacity = 1;
            oRight.style.opacity = 1;
        }

        oLeft.onmouseout = function () {
            oCover.style.opacity = 0;
            oRight.style.opacity = 0;
        }


        oLeft.onmousemove = function (e) {
            var x = e.offsetX - r;
            var y = e.offsetY - r;
            if (x < 0) x = 0;
            if (y < 0) y = 0;
            if (x > maxX) x = maxX;
            if (y > maxY) y = maxY;

            oCover.style.cssText += `left:${x}px;top:${y}px`;
            oRightImg.style.cssText += `left:${-x * scale}px;top:${-y * scale}px`;
        }


    </script>




</body>

</html>